package com.snack.shop.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.shop.common.Result;
import com.snack.shop.service.SettlementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 结算控制器
 */
@Tag(name = "结算管理")
@RestController
@RequestMapping("/pickup")
@RequiredArgsConstructor
public class SettlementController {

    private final SettlementService settlementService;

    @Operation(summary = "申请结算")
    @PostMapping("/order/{id}/apply-settlement")
    public Result<String> applySettlement(
            @PathVariable Long id,
            @RequestBody Map<String, String> body
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        String paymentMethod = body.get("paymentMethod");
        String paymentAccount = body.get("paymentAccount");
        String paymentQrCode = body.get("paymentQrCode");
        String paymentPhone = body.get("phone");
        String remark = body.get("remark");
        
        settlementService.applySettlement(id, userId, paymentMethod, paymentAccount, paymentQrCode, paymentPhone, remark);
        return Result.success("申请成功");
    }

    @Operation(summary = "获取收益统计")
    @GetMapping("/settlement/statistics")
    public Result<Map<String, Object>> statistics() {
        Long userId = StpUtil.getLoginIdAsLong();
        Map<String, Object> stats = settlementService.getStatistics(userId);
        return Result.success(stats);
    }

    @Operation(summary = "获取结算记录")
    @GetMapping("/settlement/records")
    public Result<Page<Map<String, Object>>> records(
            @Parameter(description = "结算状态") @RequestParam(required = false) Integer settlementStatus,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        Page<Map<String, Object>> page = settlementService.getRecords(userId, settlementStatus, current, size);
        return Result.success(page);
    }

    @Operation(summary = "获取结算信息（商家查看）")
    @SaCheckRole("merchant")
    @GetMapping("/settlement/info/{id}")
    public Result<Map<String, Object>> getSettlementInfo(@PathVariable Long id) {
        Map<String, Object> info = settlementService.getSettlementInfo(id);
        return Result.success(info);
    }

    @Operation(summary = "商家确认结算")
    @SaCheckRole("merchant")
    @PostMapping("/settlement/settle/{id}")
    public Result<String> settleOrder(@PathVariable Long id) {
        settlementService.settleOrder(id);
        return Result.success("结算成功");
    }

    @Operation(summary = "审核结算（管理员）")
    @SaCheckRole("merchant")
    @PostMapping("/order/{id}/settlement/approve")
    public Result<String> approveSettlement(
            @PathVariable Long id,
            @RequestBody Map<String, Object> body
    ) {
        Boolean approved = (Boolean) body.get("approved");
        String remark = (String) body.get("remark");
        
        settlementService.approveSettlement(id, approved, remark);
        return Result.success("审核完成");
    }

    @Operation(summary = "获取待审核结算列表（管理员）")
    @SaCheckRole("merchant")
    @GetMapping("/settlement/pending")
    public Result<Page<Map<String, Object>>> pendingList(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size
    ) {
        Page<Map<String, Object>> page = settlementService.getPendingList(current, size);
        return Result.success(page);
    }
}

